package cn.edu.scau.nursingcenter.controller;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


import cn.edu.scau.nursingcenter.bean.ResultBean;
import cn.edu.scau.nursingcenter.config.Audience;
import cn.edu.scau.nursingcenter.entiry.Admin;
import cn.edu.scau.nursingcenter.entiry.User;
import cn.edu.scau.nursingcenter.service.impl.AdminServiceImpl;
import cn.edu.scau.nursingcenter.service.impl.UserServiceImpl;
import cn.edu.scau.nursingcenter.util.JWTTokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 登录控制器LoginController
 * @author feng
 *
 */
@Api(tags = "登录控制器")
@RequestMapping(value = "login")
@RestController
public class LoginController {

	@Autowired 
	private UserServiceImpl userServiceImpl;
	@Autowired
	private AdminServiceImpl adminServiceImpl;
	@Autowired
	private Audience audience;
	/**
	  * 检查用户登录方法
	 * @param user
	 * @return JSON数据
	 */
	@ApiOperation(value = "检查用户登录方法")
	@PostMapping(value = "/check_user")
	@ResponseBody
	public ResultBean check_user(@Validated @RequestBody User user, HttpServletResponse response) {
		ResultBean resultBean = null;
		User targetUser = userServiceImpl.login(user.getUser_name(), user.getUser_password());
		targetUser.setUser_password("********");
		if(targetUser != null) {
			String token = JWTTokenUtil.createJWT(""+targetUser.getUser_id(), targetUser.getUser_name(), audience);
			response.setHeader("Access-Control-Expose-Headers",JWTTokenUtil.AUTH_HEAD_KEY);
			response.setHeader(JWTTokenUtil.AUTH_HEAD_KEY,token);
			resultBean = new ResultBean(200, true, "登录成功",targetUser);
		}else {
			resultBean = new ResultBean(5009, false, "登录失败", null);
		}
		return resultBean;
	}
	
	/**
	 * 检查管理员登录方法
	 * @param admin
	 * @param response
	 * @return
	 */
	@ApiOperation(value = "检查管理员登录方法")
	@PostMapping(value = "/check_admin")
	@ResponseBody
	public ResultBean check_admin(@Validated @RequestBody Admin admin, HttpServletResponse response) {
		ResultBean resultBean = null;
		Admin targetadAdmin = adminServiceImpl.login(admin.getAdmin_name(), admin.getAdmin_password());
		targetadAdmin.setAdmin_password("********");
		if(targetadAdmin != null) {
			String token = JWTTokenUtil.createJWT(""+targetadAdmin.getAdmin_id(), targetadAdmin.getAdmin_name(), audience);
			response.setHeader("Access-Control-Expose-Headers",JWTTokenUtil.AUTH_HEAD_KEY);
			response.setHeader(JWTTokenUtil.AUTH_HEAD_KEY,token);
			resultBean = new ResultBean(200, true, "登录成功",targetadAdmin);
		}else {
			resultBean = new ResultBean(5009, false, "登录失败", null);
		}
		return resultBean;
	}
}
